package com.yangxinquan.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import java.util.TreeMap;

public class GameJFrame extends JFrame implements KeyListener , ActionListener {
    private int[][] initArr = new int[4][4];
    //不清楚哪里赋值的bug,正确二维数组成这样了
    private int[][] data = {
            {1, 5, 9, 13},
            {2, 6, 10, 14},
            {3, 7, 11, 15},
            {4, 8, 12, 16}
    };
    //step记录步数
    private int step=0;
    //x和y记录0号图片的位置
    private int x = 3, y = 3;
    //照片路径
    private String path = "image\\girl\\girl1\\";

    //继承父类窗体
    public GameJFrame() {
        //初始化界面
        initJFrame();

        //初始化菜单
        initJMenuBar();

        //初始化打乱数组
        initImageArr();

        //初始化照片
        initImage();

        //显示要放到最后面
        this.setVisible(true);
    }

    private void initImageArr() {
        //需求:将一维数组的0~15打乱顺序
        //并将打乱后的一维数组赋值给二维数组
        int[] tempArr = {16,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        //打乱
        Random r = new Random();
        for (int i = 0; i < tempArr.length; i++) {
            int index = r.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
        }
        for (int i = 0; i < tempArr.length; i++) {
            initArr[i / 4][i % 4] = tempArr[i];
            if (initArr[i / 4][i % 4] == 16) {
                x = i / 4;
                y = i % 4;
            }
        }
    }

    private void initImage() {
        //清除旧图片
        this.getContentPane().removeAll();
        if(victor()){
            JLabel winjLabel=new JLabel(new ImageIcon("image\\win.png"));
            winjLabel.setBounds(203,283,197,73);
            this.getContentPane().add(winjLabel);
        }
        JLabel stepCount=new JLabel("步数"+step);
        stepCount.setBounds(50,30,100,20);
        this.getContentPane().add(stepCount);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                //创建Imageicon的对象
                //ImageIcon icon=new ImageIcon("D:\\java_test\\PuzzleGame\\image\\girl\\girl1\\1.jpg");
                //创建JLabel的对象(管理图片)
                int num = initArr[i][j];
                if(num==16){
                    continue;
                }
                //绝对路径,从盘符开始找
                //相对路径,根据项目找的:jdk18之前从项目开始找,18之后从当前模块找
                //D:\java_test\PuzzleGame\image\girl\girl1\1.jpg
                JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));
                jLabel.setBounds(105 * i + 83, 105 * j + 134, 105, 105);
                //设置图片边框
                //0:突出
                //1:凹陷
                jLabel.setBorder(new BevelBorder(0));
                this.getContentPane().add(jLabel);
            }
        }
        //设置背景图片
        ImageIcon bg = (new ImageIcon("image\\background.png"));
        JLabel backGround = new JLabel(bg);
        backGround.setBounds(40, 40, 508, 560);
        this.getContentPane().add(backGround);

        //刷新
        this.getContentPane().repaint();
    }

    //功能:重新游戏,重新登录,关闭游戏
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reloginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    //关于我们:公众号
    JMenuItem accountItem = new JMenuItem("公众号");

    private void initJMenuBar() {
        //菜单
        JMenuBar jMenuBar = new JMenuBar();
        //模块(功能,关于我们)
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");

        replayItem.addActionListener(this);
        reloginItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);

        functionJMenu.add(replayItem);
        functionJMenu.add(reloginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);

        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        //界面宽高
        this.setSize(603, 680);
        //界面的标题
        this.setTitle("拼图小游戏 v1.0");
        //界面置顶
        this.setAlwaysOnTop(true);
        //界面居中
        this.setLocationRelativeTo(null);
        //关闭模式
        this.setDefaultCloseOperation(3);
        //取消内部居中放置
        this.setLayout(null);
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    //按住不松时,如果是A,查看答案
    @Override
    public void keyPressed(KeyEvent e) {
        if(victor()){
            return;
        }
        int code = e.getKeyCode();
        if (code == 65) {
            //清空原有图片
            this.getContentPane().removeAll();
            //加载原图片
            JLabel jLabel = new JLabel(new ImageIcon(path + "all.jpg"));
            jLabel.setBounds(83, 134, 420, 420);
            this.getContentPane().add(jLabel);
            //加载背景图片
            JLabel jLabel2 = new JLabel(new ImageIcon("image\\background.png"));
            jLabel2.setBounds(40, 40, 508, 560);
            this.getContentPane().add(jLabel2);
            //刷新界面
            this.getContentPane().repaint();
        }
    }

    private void getAns() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                initArr[i][j]=data[i][j];
            }
        }
        x = 3;
        y = 3;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victor()){
            return;
        }
        //松开的时候实现移动功能
        int code = e.getKeyCode();
        System.out.println(code);
        int tempx = 0, tempy = 0;
        switch (code) {
            case 37 -> {
                System.out.println("左移");
                tempx = x - 1;
                tempy = y;
                removeImage(tempx, tempy);
            }
            case 38 -> {
                System.out.println("上移");
                tempx = x;
                tempy = y - 1;
                removeImage(tempx, tempy);
            }
            case 39 -> {
                System.out.println("右移");
                tempx = x + 1;
                tempy = y;
                removeImage(tempx, tempy);
            }
            case 40 -> {
                System.out.println("下移");
                tempx = x;
                tempy = y + 1;
                removeImage(tempx, tempy);
            }
            case 65 -> {
                //恢复原有图片
                initImage();
            }
            case 87 -> {
                //获胜
                getAns();
                initImage();
            }
        }

    }

    private void removeImage(int tempx, int tempy) {
        if (check(tempx, tempy) == false) {
            return;
        }
        step++;
        initArr[x][y] = initArr[tempx][tempy];
        initArr[tempx][tempy] = 0;
        x = tempx;
        y = tempy;
        initImage();
    }

    private boolean check(int tx, int ty) {
        if (tx < 0 || ty < 0 || tx > 3 || ty > 3) {
            return false;
        }
        return true;
    }

    private boolean victor(){
        for (int i = 0; i < initArr.length; i++) {
            for (int j = 0; j < initArr[i].length; j++) {
                if(initArr[i][j]!=data[i][j]){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj=e.getSource();
        if(obj==replayItem){
            System.out.println("重新游戏");
            //0清空计步器
            step=0;
            //1打乱二维数组照片
            initImageArr();
            //2加载图片
            initImage();

        }else if(obj==reloginItem){
            System.out.println("重新登录");

            this.setVisible(false);
            new LoginJFrame();

        }else if(obj==closeItem) {
            System.out.println("关闭游戏");
            System.exit(0);
        }else if(obj==accountItem) {
            System.out.println("打钱");
            JDialog jDialog=new JDialog();

            JLabel jLabel=new JLabel(new ImageIcon("image\\233.jpg"));
            jLabel.setBounds(0,0,258,351);
            jLabel.setVisible(true);

            jDialog.add(jLabel);

            jDialog.setBounds(0,0,258,371);
            jDialog.setTitle("打钱");

            jDialog.setLocationRelativeTo(null);
            jDialog.setAlwaysOnTop(true);
            //必须关闭弹窗才能做其他事
            jDialog.setModal(true);

            jDialog.setVisible(true);
        }
    }
}
